<!DOCTYPE html>
<html lang="en">
	<head>
		<title>threejs webgl - materials - hdr environment mapping</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background-color: #000000;
				margin: 0px;
				overflow: hidden;
				font-family:Monospace;
				font-size:13px;
				text-align:center;
				font-weight: bold;
			}
			a {
				color:#00ff78;
			}

			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				display:block;
			}
			.dg.ac {
				z-index: 1 !important; /* FIX DAT.GUI */
			}
		</style>
	</head>
	<body>

		<script src="../build/three.js"></script>
		<script src="js/controls/OrbitControls.js"></script>
		<script src="js/loaders/RGBELoader.js"></script>
		<script src="js/loaders/HDRCubeTextureLoader.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script src="js/pmrem/PMREMGenerator.js"></script>
		<script src="js/pmrem/PMREMCubeUVPacker.js"></script>
		<script src="js/libs/dat.gui.min.js"></script>

		<script src="js/postprocessing/EffectComposer.js"></script>
		<script src="js/postprocessing/RenderPass.js"></script>
		<script src="js/postprocessing/MaskPass.js"></script>
		<script src="js/postprocessing/ShaderPass.js"></script>
		<script src="js/shaders/CopyShader.js"></script>
		<script src="js/shaders/FXAAShader.js"></script>
		<script src="js/shaders/ConvolutionShader.js"></script>
		<script src="js/shaders/LuminosityHighPassShader.js"></script>
		<script src="js/postprocessing/UnrealBloomPass.js"></script>

		<div id="info">
			<a href="http://threejs.org" target="_blank">three.js</a> - Bloom pass by <a href="http://eduperiment.com" target="_blank">Prashant Sharma</a> and <a href="https://clara.io" target="_blank">Ben Houston</a><br/><br/>
			This Bloom Pass is inspired by the bloom pass of the Unreal Engine. It creates a mip map chain of bloom textures and blur them <br>
			with different radii. Because of the weigted combination of mips, and since larger blurs are done on higher mips this bloom <br>
			is better in quality and performance.<br>
		</div>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;
			var params = {
				projection: 'normal',
				background: false,
				exposure: 1.0,
				bloomStrength: 1.5,
				bloomThreshold: 0.85,
				bloomRadius: 0.4
			};
			var camera, scene, renderer, controls, objects = [];
			var effectFXAA, bloomPass, renderScene;
			var hdrCubeMap;
			var composer;
			var standardMaterial;
			var hdrCubeRenderTarget;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.set( 0.0, 35, 35 * 3.5 );

				scene = new THREE.Scene();

				renderer = new THREE.WebGLRenderer( { antialias: false } );
				renderer.setClearColor( new THREE.Color( 0x111111 ) );
				renderer.toneMapping = THREE.LinearToneMapping;

				standardMaterial = new THREE.MeshStandardMaterial( {
					map: null,
					color: 0xffffff,
					metalness: 1.0,
					shading: THREE.SmoothShading
				} );

				var geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
				var torusMesh1 = new THREE.Mesh( geometry, standardMaterial );
				torusMesh1.position.x = 0.0;
				torusMesh1.castShadow = true;
				torusMesh1.receiveShadow = true;
				scene.add( torusMesh1 );
				objects.push( torusMesh1 );

				var textureLoader = new THREE.TextureLoader();
				textureLoader.load( "./textures/roughness_map.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 9, 2 );
					standardMaterial.roughnessMap = map;
					standardMaterial.bumpMap = map;
					standardMaterial.needsUpdate = true;
				} );

				var genCubeUrls = function( prefix, postfix ) {
					return [
						prefix + 'px' + postfix, prefix + 'nx' + postfix,
						prefix + 'py' + postfix, prefix + 'ny' + postfix,
						prefix + 'pz' + postfix, prefix + 'nz' + postfix
					];
				};

				var hdrUrls = genCubeUrls( "./textures/cube/pisaHDR/", ".hdr" );
				new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, hdrUrls, function ( hdrCubeMap ) {

					var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
					pmremGenerator.update( renderer );

					var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
					pmremCubeUVPacker.update( renderer );

					hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;

				} );
				// Lights

				scene.add( new THREE.AmbientLight( 0x222222 ) );

				var spotLight = new THREE.SpotLight( 0xffffff );
				spotLight.position.set( 50, 100, 50 );
				spotLight.angle = Math.PI / 7;
				spotLight.penumbra = 0.8;
				spotLight.castShadow = true;
				scene.add( spotLight );

				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.shadowMap.enabled = true;
				container.appendChild( renderer.domElement );

				renderScene = new THREE.RenderPass(scene, camera);

		    // renderScene.clear = true;
		    effectFXAA = new THREE.ShaderPass(THREE.FXAAShader);
		    effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight );

				var copyShader = new THREE.ShaderPass(THREE.CopyShader);
				copyShader.renderToScreen = true;

		    bloomPass = new THREE.UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);//1.0, 9, 0.5, 512);
				composer = new THREE.EffectComposer(renderer);
		    composer.setSize(window.innerWidth, window.innerHeight);
		    composer.addPass(renderScene);
				composer.addPass(effectFXAA);
		    composer.addPass(bloomPass);
				composer.addPass(copyShader);
				//renderer.toneMapping = THREE.ReinhardToneMapping;
				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				stats = new Stats();
				container.appendChild( stats.dom );

				controls = new THREE.OrbitControls( camera, renderer.domElement );
				controls.target.set( 0, 0, 0 );
				controls.update();

				window.addEventListener( 'resize', onWindowResize, false );

				var gui = new dat.GUI();

				gui.add( params, 'exposure', 0.1, 2 );
				gui.add( params, 'bloomThreshold', 0.0, 1.0 ).onChange( function(value) {
		        bloomPass.threshold = Number(value);
		    });
				gui.add( params, 'bloomStrength', 0.0, 3.0 ).onChange( function(value) {
		        bloomPass.strength = Number(value);
		    });
				gui.add( params, 'bloomRadius', 0.0, 1.0 ).onChange( function(value) {
		        bloomPass.radius = Number(value);
		    });
				gui.open();

			}

			function onWindowResize() {

				var width = window.innerWidth;
				var height = window.innerHeight;

				camera.aspect = width / height;
				camera.updateProjectionMatrix();

				renderer.setSize( width, height );
				composer.setSize( width, height );
				effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight );
			}

			//

			function animate() {

				requestAnimationFrame( animate );

				stats.begin();
				render();
				stats.end();

			}

			function render() {

				if ( standardMaterial !== undefined ) {

					standardMaterial.roughness = 1.0;
					standardMaterial.bumpScale = - 0.05;

					var newEnvMap = standardMaterial.envMap;
					newEnvMap = hdrCubeRenderTarget ? hdrCubeRenderTarget.texture : null;

					if( newEnvMap !== standardMaterial.envMap ) {

						standardMaterial.envMap = newEnvMap;
						standardMaterial.needsUpdate = true;

					}

				}

				renderer.toneMappingExposure = Math.pow( params.exposure, 4.0 );

				var timer = Date.now() * 0.00025;

				camera.lookAt( scene.position );

				for ( var i = 0, l = objects.length; i < l; i ++ ) {

					var object = objects[ i ];
					object.rotation.y += 0.005;

				}

				// renderer.render( scene, camera );
				composer.render();
			}

		</script>

	</body>
</html>
